package com.sise.wangzhan.command;

import com.netflix.hystrix.HystrixCollapser;
import com.netflix.hystrix.HystrixCommand;

import java.util.Collection;
import java.util.Map;

/**
 * @Classname MyHystrixCollapser
 * @Description TODO    合并处理器，用于收集请求，以及处理结果
 *                      第一个类型为批处理返回的结果类型
 *                      第二个为单请求返回的结果类型
 *                      第三个为请求参数类型
 * @Date 2020/4/29 20:19
 * @Created by wangzhan
 */
public class MyHystrixCollapser extends HystrixCollapser<Map<String, CollapserCommand.Person>, CollapserCommand.Person, String> {

    String personName;

    public MyHystrixCollapser() {
    }

    public MyHystrixCollapser(String personName) {
        this.personName = personName;
    }

    @Override
    public String getRequestArgument() {
        return personName;
    }

    @Override
    protected HystrixCommand<Map<String, CollapserCommand.Person>> createCommand(Collection<CollapsedRequest<CollapserCommand.Person, String>> requests) {
        return new CollapserCommand(requests);
    }

    @Override
    protected void mapResponseToRequests(Map<String, CollapserCommand.Person> stringPersonMap, Collection<CollapsedRequest<CollapserCommand.Person, String>> requests) {
        //让请求与结果关联
        for (CollapsedRequest<CollapserCommand.Person,String> request : requests){
            //获取单个响应返回的结果
            CollapserCommand.Person singleResult = stringPersonMap.get(request.getArgument());
            //关联到请求中
            request.setResponse(singleResult);
        }
    }
}
